<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拷贝</title>
    <!-- 
        浅拷贝和深拷贝
    1. 浅拷贝只是拷贝一层，更深层次对象级别的只拷贝引用。
    2. 深拷贝拷贝多层，每一级别的数据都会拷贝。
    3. Object.assign(target, ...sources) es6 新增方法可以浅拷贝
     -->
</head>
<body>
    
</body>
<script>
    console.log('**********浅拷贝*************');
    var obj = {
        id: 1,
        name: 'andy' ,
        msg: {
            age: 18
        }
    }
    var o = {}
    Object.assign(o,obj)
    console.log(o);
    o.msg.age = 20
    console.log(o);
    console.log(obj);
    console.log('*************深拷贝**********');
    var obj1 = {
        id: 2,
        name: 'pink',
        msg: {
            age:18
        },
        color:['red','blue','green']
    }
    var o = {}
    function deepCopy(newObj,oldObj){
        for(var k in oldObj){
            // console.log(k);
            // console.log(oldObj[k]);
            var item = oldObj[k]
            console.log(item);
            if(item instanceof Array){
                newObj[k] = [newObj[k],item]
                deepCopy()
            }else if(item instanceof Object){
                newObj[k] = {}
                deepCopy(newObj[k],item)
            }else{
                newObj[k] = item
            }
        }
    }
    deepCopy(o,obj1)
    console.log(o);
    
    var arr = []
    console.log(arr instanceof Object);
    o.msg.age =23
    console.log(o);
    console.log(obj1);

</script>
</html>